package com.tao.mvpbaselibrary.basic.widget.countdown;

/**
 * @author chengang
 * @date 2019-06-24
 * @email chenganghonor@gmail.com
 * @QQ 1410488687
 * @package_name com.nj.baijiayun.basic.widget.countdown
 * @describe
 */
public class CountDownHelper {

    private static final int MIN_IN_mill = 1000;
    private static final int MIN_IN_SEC = 60;
    private static final int HOUR_IN_SEC = MIN_IN_SEC * 60;

    public static String formatDuration(long ms) {
        int duration = (int) (ms / 1000);

        int h = 0;
        int m = 0;

        if (duration >= HOUR_IN_SEC) {
            h = duration / HOUR_IN_SEC;
            duration -= h * HOUR_IN_SEC;
        }
        if (duration >= MIN_IN_SEC) {
            m = duration / MIN_IN_SEC;
            duration -= m * MIN_IN_SEC;
        }

        StringBuilder stringBuilder = new StringBuilder("");
        if (h < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(h).append(":");
        if (m < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(m).append(":");
        if (duration < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(duration);
        return stringBuilder.toString();
    }

    public static String formatDurationCha(long ms, boolean nullAppend) {
        int duration = (int) (ms / 1000);

        int h = 0;
        int m = 0;

        if (duration >= HOUR_IN_SEC) {
            h = duration / HOUR_IN_SEC;
            duration -= h * HOUR_IN_SEC;
        }
        if (duration >= MIN_IN_SEC) {
            m = duration / MIN_IN_SEC;
            duration -= m * MIN_IN_SEC;
        }

        StringBuilder stringBuilder = new StringBuilder("");

        if (h <= 0 && nullAppend || h > 0) {
//            if (h < 10) {
//                stringBuilder.append("0");
//            }
            stringBuilder.append(h).append("时");
        }
        if (m <= 0 && nullAppend || m > 0) {
//            if (m < 10) {
//                stringBuilder.append("0");
//            }
            stringBuilder.append(m).append("分");
        }

        if (duration <= 0 && nullAppend || duration > 0) {
//            if (duration < 10) {
//                stringBuilder.append("0");
//            }
            stringBuilder.append(duration) .append("秒");
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.err.println(formatDuration(5 * 60 * 60 * 1000));
        int time = 5 * 60 * 60 * 1000;
        while (true) {
            time -= 1000;
            System.err.println(timeCount(time));
            System.err.println(formatDuration(time));
            System.err.println("---");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    private static String timeCount(long time) {

        int h;
        int m;
        int s;
//        System.err.println(time);
        h = getH(time);
//        System.err.println(h);
        m = getM(time);
//        System.err.println(m);
        s = getS(time);
//        System.err.println(s);

        StringBuilder stringBuilder = new StringBuilder("");
        if (h < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(h).append(":");
        if (m < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(m).append(":");
        if (s < 10) {
            stringBuilder.append("0");
        }
        stringBuilder.append(s);
        return stringBuilder.toString();
    }

    public static int getS(long time) {
        return (int) (time % (60 * 1000) / 1000);
    }

    public static int getM(long time) {
        return (int) (time % (60 * 60 * 1000) / (60 * 1000));
    }

    public static int  getH(long time ) {
        return (int) (time / (60 * 60 * 1000));
    }
}
